package com.ServiceImpl;


import com.BaseMapper.MyBaseMapper;
import com.Service.IService;

import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.common.Mapper;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * 基础抽象
 *
 * @param <T>
 */
public abstract class BaseSevice<T> implements IService<T> {

    @Autowired
    protected MyBaseMapper<T> mapper;

    public Mapper<T> getMapper() {
        return mapper;
    }

    /**
     * 根据主键查实体
     * @param key
     * @return
     */
    @Override
    public T selectByKey(Object key) {
        return mapper.selectByPrimaryKey(key);
    }

    /**
     * 保存实体
     * @param entity
     * @return
     */
    @Override
    public int save(T entity) {
        return mapper.insert(entity);
    }

    /**
     * 保存实体 非空不保存
     * @param entity
     * @return
     */
    @Override
    public int saveExcludeNull(T entity) {
        return mapper.insertSelective(entity);
    }

    /**
     * 更新实体
     * @param entity
     * @return
     */
    @Override
    public int update(T entity) {
        return mapper.updateByPrimaryKey(entity);
    }

    /**
     * 更新实体, 为null的字段不更新
     *
     * @param entity
     * @return
     */
    @Override
    public int updateExcludeNull(T entity) {
        return mapper.updateByPrimaryKeySelective(entity);
    }

    /**
     * 删除实体
     *
     * @param entity
     * @return
     */
    @Override
    public int delete(T entity) {
        return mapper.delete(entity);
    }

    /**
     * 根据主键删除实体
     *
     * @param key
     * @return
     */
    @Override
    public int deleteByKey(Object key) {
        return mapper.deleteByPrimaryKey(key);
    }

    /**
     * 逻辑删除
     *
     * @param entity
     * @return
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    @Override
    public int logicalDelete(T entity) throws IllegalAccessException, InvocationTargetException {
       // BeanUtils.setProperty(entity, "delF", 1);
        return mapper.updateByPrimaryKeySelective(entity);
    }

    /**
     * 根据主键逻辑删除
     *
     * @param key
     * @return
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    @Override
    public int logicalDeleteByKey(Object key) throws IllegalAccessException, InvocationTargetException {
        T entity = mapper.selectByPrimaryKey(key);
        return this.logicalDelete(entity);
    }

    /**
     * 批量保存
     *
     * @param entitys
     * @return
     */
    @Override
    public int batchSave(List<T> entities) {
		/*if (entities != null && !entities.isEmpty()) {
			int[] effecteds = new int[entities.size()];
			for (int i = 0; i < effecteds.length; i++) {
				effecteds[i] = this.save(entities.get(i));
			}
			return effecteds;
		} else {
			return new int[0];
		}*/
        return mapper.insertList(entities);
    }

    /**
     * 批量保存
     *
     * @param entitys
     * @return
     */
    @Override
    public int batchSave(List<T> entities, int batchSize) {
        int effecteds = 0;

        if (entities != null && !entities.isEmpty()) {
            int left = entities.size() % batchSize;
            int batch = 0;
            if (left == 0) {
                batch = entities.size() / batchSize;
            } else {
                batch = entities.size() / batchSize + 1;
            }

            int fromIndex = 0;
            int toIndex = 0;

            for (int i = 0; i < batch; i++) {
                fromIndex = batchSize * i;
                toIndex = fromIndex + batchSize;
                if (toIndex >= entities.size()) {
                    toIndex = entities.size();
                }
                effecteds += mapper.insertList(entities.subList(fromIndex, toIndex));
            }
        }

        return effecteds;
    }

    /**
     * 批量更新
     *
     * @param entitys
     * @return
     */
    @Override
    public int[] batchUpdate(List<T> entities) {
        if (entities != null && !entities.isEmpty()) {
            int[] effecteds = new int[entities.size()];
            for (int i = 0; i < effecteds.length; i++) {
                effecteds[i] = this.update(entities.get(i));
            }
            return effecteds;
        } else {
            return new int[0];
        }
    }

    /**
     * 循环批量更新 null字段不更新
     *
     * @param entities
     * @return
     */
    @Override
    public int batchUpdateExcludeNull(List<T> entities) {
        int effecteds = 0;
        if (entities != null && !entities.isEmpty()) {
            for (int i = 0; i < entities.size(); i++) {
                effecteds += this.updateExcludeNull(entities.get(i));
            }
        }
        return effecteds;
    }


    /**
     * 批量删除
     *
     * @param entities
     * @return
     */
    public int[] batchDelete(List<T> entities) {
        if (entities != null && !entities.isEmpty()) {
            int[] effecteds = new int[entities.size()];
            for (int i = 0; i < effecteds.length; i++) {
                effecteds[i] = this.delete(entities.get(i));
            }
            return effecteds;
        } else {
            return new int[0];
        }
    }

    /**
     * 逻辑批量删除
     *
     * @param entities
     * @return
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    @Override
    public int[] batchLogicalDelete(List<T> entities) throws IllegalAccessException, InvocationTargetException {
        if (entities != null && !entities.isEmpty()) {
            int[] effecteds = new int[entities.size()];
            for (int i = 0; i < effecteds.length; i++) {
                effecteds[i] = this.logicalDelete(entities.get(i));
            }
            return effecteds;
        } else {
            return new int[0];
        }
    }

    /**
     * 根据Example查询列表
     *
     * @param example
     * @return
     */
    @Override
    public List<T> selectByExample(Object example) {
        return mapper.selectByExample(example);
    }
}
